package com.love.data.structure.queues;

/**
 * 队列
 * 基于链表
 *
 * @author gd liu
 * @version 1.0
 * @date 2021/9/22
 */
public class LinkedQueues {

    /**
     * 队列默认的大小
     */
    private static final int MAX_SIZE = 16;
    /**
     * 队列最大
     */
    private final int maxSize;
    /**
     * 头
     */
    private Node header;
    /**
     * 尾
     */
    private Node end;
    /**
     * 元素数
     */
    private int nItems;

    /**
     * 节点
     */
    static class Node {
        /**
         * 数据
         */
        private final int data;
        /**
         * 下个节点
         */
        private Node next;

        public Node() {
            this(0, null);
        }

        public Node(int data) {
            this(data, null);
        }

        public Node(int data, Node node) {
            this.data = data;
            this.next = node;
        }

    }

    /**
     * 创建队列
     *
     * @author gd liu
     * @date 2021/9/22 11:38
     */
    public LinkedQueues() {
        this(MAX_SIZE);
    }

    /**
     * 创建队列
     *
     * @author gd liu
     * @date 2021/9/22 11:38
     */
    public LinkedQueues(int size) {
        header = end = new Node();
        maxSize = size;
    }

    /**
     * 创建队列
     *
     * @author gd liu
     * @date 2021/9/22 11:38
     */
    public LinkedQueues(int item, int size) {
        header = end = new Node(item);
        maxSize = size;
    }

    /**
     * 添加元素
     *
     * @param item 元素
     * @return boolean
     * @author gd liu
     * @date 2021/9/22 11:41
     */
    public boolean add(int item) {
        if (isFull()) {
            return false;
        }
        Node node = new Node(item);
        if (isEmpty()) {
            header = node;
        }
        nItems++;
        end.next = node;
        end = node;
        return true;
    }

    /**
     * 删除元素
     *
     * @return boolean
     * @author gd liu
     * @date 2021/9/22 11:41
     */
    public boolean remove() {
        if (isEmpty()) {
            return false;
        }
        nItems--;
        Node destroy = header;
        header = header.next;
        /* GC生效 */
        destroy = null;

        if (isEmpty()) {
            header = end;
        }
        return true;
    }

    /**
     * 是否空
     *
     * @return boolean
     * @author gd liu
     * @date 2021/9/22 11:38
     */
    public boolean isEmpty() {
        return nItems == 0;
    }

    /**
     * 是否满
     *
     * @return boolean
     * @author gd liu
     * @date 2021/9/22 11:38
     */
    public boolean isFull() {
        return nItems == maxSize;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder("[");
        Node node = header;
        while (node != null) {
            stringBuilder.append(node.data).append(",");
            node = node.next;
        }
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        stringBuilder.append("]");
        return "LinkedQueues{" +
                "queues=" + stringBuilder +
                ", header=" + header +
                ", end=" + end +
                ", nItems=" + nItems +
                '}';
    }

}
